/**
 * 北京钉图互动科技 all right reserver
 */
package com.impress.commons.vo;

import java.util.List;

import static com.google.common.base.Preconditions.checkArgument;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * @author Wangyi
 * @since 2016年6月13日
 */
@ApiModel(value = "数据分页")
public class Page<T> extends Chunk<T> {

	private static final long serialVersionUID = -6054398270610480521L;

	@ApiModelProperty(value = "页号,1开始", required = true)
	private int number;

	@ApiModelProperty(value = "页尺寸,大于0", required = true)
	private int size;

	@ApiModelProperty(value = "总记录数", required = true)
	private int total;

	@ApiModelProperty(value = "总页数", required = true)
	private int totalPage;

	/**
	 * 
	 */
	public Page() {
	}

	public Page(int number, int size) {
		this(number, size, 0);
	}

	public Page(int number, int size, int total) {
		checkArgument(number > 0, "页号最小必须是1");
		checkArgument(size > 0, "每页尺寸必须大于0");
		this.number = number;
		this.size = size;
		this.total = total;
	}

	/**
	 * @return the number
	 */
	public int getNumber() {
		return number;
	}

	/**
	 * @param number the number to set
	 */
	public void setNumber(int number) {
		checkArgument(number > 0, "页号最小必须是1");
		this.number = number;
	}

	/**
	 * @return the size
	 */
	public int getSize() {
		return size;
	}

	/**
	 * @param size the size to set
	 */
	public void setSize(int size) {
		checkArgument(size > 0, "每页尺寸必须大于0");
		this.size = size;
		this.totalPage = 0;
	}

	/**
	 * @return the total
	 */
	public int getTotal() {
		return total;
	}

	/**
	 * @param total the total to set
	 */
	public void setTotal(int total) {
		this.total = total;
		this.totalPage = 0;
	}

	/**
	 * @return the totalPage
	 */
	public int getTotalPage() {
		if (totalPage == 0 && total != 0 && size != 0) {
			this.totalPage = calcPageLength(total, size);
		}
		return totalPage;
	}

	/**
	 * @param totalPage the totalPage to set
	 */
	protected void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	static int calcPageLength(int total, int size) {
		int pageLen = 0;
		if (total != 0 && size != 0) {
			pageLen = (int) Math.ceil(total * 1d / size);
		}
		return pageLen;
	}

	public void setContent(List<T> content, int total) {
		this.setTotal(total);
		this.setContent(content);
	}

}
